unit comprasMaterial;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Provider, SqlExpr, StdCtrls, DB, DBClient, DBLocal, DBLocalS,
  Buttons, Grids, DBGrids, RXDBCtrl, Mask, ToolEdit, DateUtils, DBCtrls,
  ppDB, ppBands, ppReport, ppStrtch, ppSubRpt, ppCtrls, ppPrnabl, ppClass,
  ppCache, ppProd, ppComm, ppRelatv, ppDBPipe, ppTypes, ppVar, Menus;

type
  TfrmComprasMaterial = class(TForm)
    lblFechaIni: TLabel;
    edtFechaIni: TDateEdit;
    lblFechaFin: TLabel;
    edtFechaFin: TDateEdit;
    grdMaterial: TRxDBGrid;
    filtro: TEdit;
    Label1: TLabel;
    btnFiltrado: TSpeedButton;
    cds: TSQLClientDataSet;
    s: TDataSource;
    cdsMATERIAL: TStringField;
    btnCargar: TButton;
    cdsSUBTOTAL: TFloatField;
    cdstotal: TAggregateField;
    Label3: TLabel;
    total: TDBText;
    cdsCANTIDAD: TFloatField;
    cdspromedio: TFloatField;
    ppComprasMaterial: TppDBPipeline;
    rpComprasMaterial: TppReport;
    ppHeaderBand1: TppHeaderBand;
    ppDBImage1: TppDBImage;
    ppLabel8: TppLabel;
    ppEmail: TppLabel;
    ppTe: TppLabel;
    ppDBText3: TppDBText;
    ppDBText6: TppDBText;
    ppLine4: TppLine;
    ppDetailBand1: TppDetailBand;
    ppFooterBand1: TppFooterBand;
    ppEmpresa: TppDBPipeline;
    ppEmpresappField1: TppField;
    ppEmpresappField2: TppField;
    ppEmpresappField3: TppField;
    ppEmpresappField4: TppField;
    ppEmpresappField5: TppField;
    ppEmpresappField6: TppField;
    ppEmpresappField7: TppField;
    ppEmpresappField8: TppField;
    ppEmpresappField9: TppField;
    ppEmpresappField10: TppField;
    ppEmpresappField11: TppField;
    ppEmpresappField12: TppField;
    ppEmpresappField13: TppField;
    ppEmpresappField14: TppField;
    ppEmpresappField15: TppField;
    sEmpresa: TDataSource;
    ppLabel1: TppLabel;
    ppDBCalc1: TppDBCalc;
    btnVistaPrevia: TBitBtn;
    ppLabel4: TppLabel;
    ppLabel5: TppLabel;
    ppLabel6: TppLabel;
    ppLabel7: TppLabel;
    ppLine1: TppLine;
    ppLine3: TppLine;
    datMaterial: TppDBText;
    datUnitario: TppDBText;
    datSubtotal: TppDBText;
    datCantidad: TppDBText;
    ppSystemVariable1: TppSystemVariable;
    cdsRAZON: TStringField;
    Label2: TLabel;
    filtroExtra: TEdit;
    btnFiltradoExtra: TSpeedButton;
    ppLabel2: TppLabel;
    ppDBText1: TppDBText;
    Label4: TLabel;
    DBText1: TDBText;
    cdsregistros: TAggregateField;
    ppLabel3: TppLabel;
    ppDBCalc2: TppDBCalc;
    lblIntervalo: TppLabel;
    PopupMenu1: TPopupMenu;
    Borrarregistro1: TMenuItem;
    btnExcel: TSpeedButton;
    procedure btnCargarClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure grdMaterialTitleClick(Column: TColumn);
    procedure grdMaterialKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure btnFiltradoClick(Sender: TObject);
    procedure filtroChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cdsCalcFields(DataSet: TDataSet);
    procedure btnVistaPreviaClick(Sender: TObject);
    procedure filtroKeyPress(Sender: TObject; var Key: Char);
    procedure grdMaterialCellClick(Column: TColumn);
    procedure filtroExtraKeyPress(Sender: TObject; var Key: Char);
    procedure filtroExtraChange(Sender: TObject);
    procedure btnFiltradoExtraClick(Sender: TObject);
    procedure rpComprasMaterialBeforePrint(Sender: TObject);
    procedure Borrarregistro1Click(Sender: TObject);
    procedure grdMaterialKeyPress(Sender: TObject; var Key: Char);
    procedure btnExcelClick(Sender: TObject);
  private
    FcolumnaSeleccionada: word;
    procedure SetcolumnaSeleccionada(const Value: word);
    { Private declarations }
  published
    property columnaSeleccionada: word read FcolumnaSeleccionada write SetcolumnaSeleccionada;
  public
    constructor CreateNuevo(AOwner: TComponent);
  end;

var
  frmComprasMaterial: TfrmComprasMaterial;

implementation

uses MDatos, Recursos;

{$R *.dfm}

constructor TfrmComprasMaterial.CreateNuevo(AOwner: TComponent);
begin
  inherited Create(Aowner);
  self.columnaSeleccionada := 0;
  self.edtFechaIni.Date := IncYear(now, -1);
  self.edtFechaFin.Date := now;
  self.ShowModal;
end;

procedure TfrmComprasMaterial.btnCargarClick(Sender: TObject);
begin
  self.cds.Close;
  self.cds.Params[0].AsString := '%'+self.filtro.Text+'%';
  self.cds.Params[1].AsString := FormatDateTime('mm/dd/yy', StrToDate(self.edtFechaIni.EditText));
  self.cds.Params[2].AsString := FormatDateTime('mm/dd/yy', incDay(StrToDate(self.edtFechaFin.EditText)));
  self.cds.Open;
end;

procedure TfrmComprasMaterial.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  self.cds.Close;
end;

procedure TfrmComprasMaterial.grdMaterialTitleClick(Column: TColumn);
begin
  if (Column.Field <> self.cdspromedio) then
    self.cds.IndexFieldNames := Column.FieldName;
end;

procedure TfrmComprasMaterial.grdMaterialKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  if (Key = VK_DELETE) then
    self.cds.Delete;
end;

procedure TfrmComprasMaterial.btnFiltradoClick(Sender: TObject);
begin
  self.filtro.Text := '';
  self.filtro.SetFocus;
  self.btnCargarClick(Sender);
end;

procedure TfrmComprasMaterial.filtroChange(Sender: TObject);
begin
  self.btnFiltrado.Enabled := (self.filtro.Text <> '');
end;

procedure TfrmComprasMaterial.FormCreate(Sender: TObject);
begin
  acomodo(self);
end;

procedure TfrmComprasMaterial.cdsCalcFields(DataSet: TDataSet);
begin
  if (MonthsBetween(self.edtFechaIni.Date, self.edtFechaFin.Date) <> 0) then
    self.cdspromedio.AsFloat := self.cdsSUBTOTAL.AsFloat / MonthsBetween(self.edtFechaIni.Date, self.edtFechaFin.Date)
  else
    self.cdspromedio.AsFloat := self.cdsSUBTOTAL.AsFloat;
end;

procedure TfrmComprasMaterial.btnVistaPreviaClick(Sender: TObject);
begin
  if (self.cds.State in [dsInactive]) then
    self.btnCargarClick(Sender);
  self.rpComprasMaterial.DeviceType := dtScreen;
  self.rpComprasMaterial.Print;
end;

procedure TfrmComprasMaterial.filtroKeyPress(Sender: TObject;
  var Key: Char);
begin
  if (Key = chr(VK_RETURN)) then
    self.btnCargarClick(Sender);
end;

procedure TfrmComprasMaterial.grdMaterialCellClick(Column: TColumn);
var
  i : word;
begin
  if (Column.Title.Font.Color <> clRed) then
  begin
    for i := 0 to self.grdMaterial.Columns.Count-1 do
      self.grdMaterial.Columns [i].Title.Font.Color := Column.Title.Font.Color;
    Column.Title.Font.Color := clRed;
    self.columnaSeleccionada := self.grdMaterial.Col-1;
  end; 
end;

procedure TfrmComprasMaterial.SetcolumnaSeleccionada(const Value: word);
begin
  FcolumnaSeleccionada := Value;
end;

procedure TfrmComprasMaterial.filtroExtraKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key = chr(VK_RETURN) then
  begin      
    if (self.filtroExtra.Text <> '') then
    begin
      if (self.grdMaterial.Columns[self.columnaSeleccionada].Field.DataType = ftString) then
        self.cds.Filter := self.grdMaterial.Columns [self.columnaSeleccionada].FieldName+' LIKE ''%'+self.filtroExtra.Text+'%'''
      else
        self.cds.Filter := self.grdMaterial.Columns [self.columnaSeleccionada].FieldName+' = '+self.filtroExtra.Text;
      self.cds.Filtered := True;
    end
    else self.cds.Filtered := False;
  end;
end;

procedure TfrmComprasMaterial.filtroExtraChange(Sender: TObject);
begin
  self.btnFiltradoExtra.Enabled := (self.filtroExtra.Text <> '');
  //tecla := chr(VK_RETURN);
  //self.filtroExtraKeyPress(Sender, tecla);
end;

procedure TfrmComprasMaterial.btnFiltradoExtraClick(Sender: TObject);
begin
  self.filtroExtra.Text := '';
  self.filtroExtra.SetFocus;
end;

procedure TfrmComprasMaterial.rpComprasMaterialBeforePrint(
  Sender: TObject);
begin
  self.lblIntervalo.Caption:= 'Del '+self.edtFechaIni.Text+' al '+self.edtFechaFin.Text;
end;

procedure TfrmComprasMaterial.Borrarregistro1Click(Sender: TObject);
var
  i: Integer;
begin
  if self.grdMaterial.SelectedRows.Count > 0 then
    for i:=0 to self.grdMaterial.SelectedRows.Count-1 do
      self.grdMaterial.SelectedRows.Delete;
end;

procedure TfrmComprasMaterial.grdMaterialKeyPress(Sender: TObject;
  var Key: Char);
begin
  if (Key = chr(VK_DELETE)) then
    self.Borrarregistro1Click(Sender);
end;

procedure TfrmComprasMaterial.btnExcelClick(Sender: TObject);
begin
   abrirEnExcel(self, self.cds, nil, nil, 'COMPRAS POR MATERIAL', 'INTERVALO DEFINIDO: del '
  +DateTimeToStr(self.edtFechaIni.Date)+' al '+DateTimeToStr(self.edtFechaFin.Date));
end;

end.
